{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "smooth-frontier",
   "metadata": {},
   "source": [
    "# Problem 14.06 Required Volume for a Gas Storage Tank for Ammonia"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "quarterly-marijuana",
   "metadata": {},
   "source": [
    "50 m^3 of liquid ammonia is stored at the conditions 50 °C and 100 bar. The vessel fails, and the contents empties into a backup containment vessel. The backup vessel has a maximum pressure of 10 bar. What volume must be vessel be to not exceed this pressure?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numerical-suspect",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is straightforward; energy is conserved and a pressure is specified. Find the amount of ammonia in the original vessel; find the molar volume of ammonia in the new vessel; and multiply that by the amount of ammonia.\n",
    "\n",
    "Ammonia is a highly non-ideal fluid, so we use a high-precision EOS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "occupational-right",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the conditions and imports\n",
    "from scipy.constants import bar\n",
    "from thermo import ChemicalConstantsPackage, PRMIX, CEOSLiquid, CoolPropLiquid, CEOSGas, CoolPropGas, FlashPureVLS\n",
    "fluid = 'ammonia'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = 50 + 273.15\n",
    "P1 = 100*bar\n",
    "P2 = 10*bar\n",
    "zs = [1]\n",
    "volume_1 = 50\n",
    "\n",
    "backend = 'HEOS'\n",
    "gas = CoolPropGas(backend, fluid, T=T1, P=1e5, zs=zs)\n",
    "liquid = CoolPropLiquid(backend, fluid, T=T1, P=1e5, zs=zs)\n",
    "\n",
    "flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "overhead-aggregate",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The thermodynamically required secondary containment volume is  433.83 m^3\n"
     ]
    }
   ],
   "source": [
    "# Flash at inlet conditions to obtain initial enthalpy\n",
    "state_1 = flasher.flash(T=T1, P=P1)\n",
    "moles = volume_1/state_1.V()\n",
    "state_2 = flasher.flash(P=P2, H=state_1.H())\n",
    "\n",
    "volume_2 = moles*state_2.V()\n",
    "print(f'The thermodynamically required secondary containment volume is {volume_2: .2f} m^3')"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
